package comWorld.action.blog;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import comWorld.blogComment.BlogComment;
import comWorld.blogComment.BlogCommentManage;
import comWorld.user.UserStatus;

/**
 * Servlet implementation class ListComment
 */
@WebServlet("/action/blog/listComment")
public class ListComment extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListComment() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserStatus status = (UserStatus) request.getSession().getAttribute("visitorStatus");
		int userId = Integer.valueOf(request.getParameter("userId"));
		int blogId = Integer.valueOf(request.getParameter("blogId"));
		int page = Integer.valueOf(request.getParameter("page"));
		int size = Integer.valueOf(request.getParameter("size"));
		List<BlogComment> comments = BlogCommentManage.getBlogComments(blogId);
		int begin, end;
		int count = comments.size() - (page - 1) * size;
		count = count > size ? size : count;
		begin = (page - 1) * size;
		end = begin + count;
		
		BlogComment[] commentsArr = comments.subList(begin, end).toArray(new BlogComment[end - begin]);
		if (userId == status.getId()) {
			for (int i = 0; i < commentsArr.length; i++) {
				commentsArr[i].setCanAdmin(true);
			}
		}
		response.getWriter().println(JSON.toJSONString(commentsArr));
	}

}
